package com.xxg.renrensearch.dao;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Indexes;
import com.xxg.renrencommon.domain.bean.CarryProductSettingInfo;
import com.xxg.renrensearch.domain.result.ShopCarryTypeCount;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.GroupOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

/**
 * 带货设置信息dao
 *
 * @author szj
 * @date 2021/11/29
 */
@Repository
public class CarryProductSettingInfoDao {
    private final MongoTemplate mongoTemplate;

    public CarryProductSettingInfoDao(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
        createCollection();
    }

    /**
     * 创建collection
     */
    private void createCollection() {
        String collectionName = "carryProductSettingInfo";
        MongoCollection<Document> collection;
        if (!mongoTemplate.getCollectionNames().contains(collectionName)) {
            collection = mongoTemplate.createCollection(CarryProductSettingInfo.class);
            createIndex(collection);
        }
    }

    /**
     * 创建索引
     *
     * @param collection 数据集
     */
    private void createIndex(MongoCollection<Document> collection) {
        collection.createIndex(Indexes.ascending("userId"));
        collection.createIndex(Indexes.ascending("shopId"));
        collection.createIndex(Indexes.ascending("spuId"));
        collection.createIndex(Indexes.compoundIndex(Indexes.ascending("userId"), Indexes.ascending("spuId")));
    }

    /**
     * 获取所有店铺带货或不带货商品数量（根据店铺、全局、权限分组）
     *
     * @return 结果
     */
    public List<ShopCarryTypeCount> findAllCarryInfo() {
        List<AggregationOperation> operations = new ArrayList<>();
        GroupOperation groupOperation = group("shopId", "type", "carryAble")
                .first("shopId").as("shopId")
                .first("type").as("type")
                .first("carryAble").as("carryAble")
                .count().as("count");
        operations.add(groupOperation);
        Aggregation agg = newAggregation(operations);
        AggregationResults<ShopCarryTypeCount> results = mongoTemplate.aggregate(agg, CarryProductSettingInfo.class,
                ShopCarryTypeCount.class);
        return results.getMappedResults();
    }

    /**
     * 根据店铺ID获取其不允许带货的SPUID列表
     *
     * @param shopId 店铺ID
     * @return 结果
     */
    public List<ShopCarryTypeCount> findCarryInfoById(Integer shopId) {
        List<AggregationOperation> operations = new ArrayList<>();
        operations.add(match(Criteria.where("shopId").is(shopId)));
        GroupOperation groupOperation = group("type", "carryAble", "carryApplyAble")
                .first("type").as("type")
                .first("carryAble").as("carryAble")
                .first("carryApplyAble").as("carryApplyAble")
                .push("spuId").as("spuIds");
        operations.add(groupOperation);
        Aggregation agg = newAggregation(operations);
        AggregationResults<ShopCarryTypeCount> results = mongoTemplate.aggregate(agg, CarryProductSettingInfo.class,
                ShopCarryTypeCount.class);
        return results.getMappedResults();
    }
}
